package com.chen.thread;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *AtomicInteger 线程安全
 *@author：email: <a href="cyb@ewppay.com"> Administrator </a> 
 *@Date 2017-2-17 上午10:35:41 
 */

public class Test {
	static AtomicInteger count = new AtomicInteger();
	
	public static class AddThread implements Runnable{

		@Override
		public void run() {
			count.addAndGet(2);
		}
		
	}
	
	public static void atomicIntegerShow(){
		ThreadPoolExecutor threadPool =  new ThreadPoolExecutor(5, 5,  
                0L, TimeUnit.MILLISECONDS,  
                new LinkedBlockingQueue<Runnable>());  
		for(int i = 0; i < 100;i++){
			threadPool.submit(new AddThread());
		}
		 try {  
	            Thread.sleep(10);  //此处等待是为了让前面的100个线程都执行完
	        } catch (InterruptedException e) {  
	            e.printStackTrace();  
	        }  
		 
		System.out.println("结果Count=" + count);
		threadPool.shutdown();
	}
	

	/**
	 * @param @param args   
	 * @return void  
	 * @throws
	 * @author cyb
	 * @date 2017-2-17
	 */
	public static void main(String[] args) {
		Test.atomicIntegerShow();
		
	}

}
